package com.wei.duowan.bbs.ui.fragment;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.presenter.BasePresenter;
import com.wei.duowan.bbs.ui.activity.AccountManagerActivity;
import com.wei.duowan.bbs.ui.activity.AttentionActivity;
import com.wei.duowan.bbs.ui.activity.DynamicActivity;
import com.wei.duowan.bbs.ui.activity.FavoriteActivity;
import com.wei.duowan.bbs.ui.activity.FriendActivity;
import com.wei.duowan.bbs.ui.activity.PlateActivity;
import com.wei.duowan.bbs.ui.activity.PostActivity;
import com.wei.duowan.bbs.ui.activity.SettingActivity;
import com.wei.duowan.bbs.ui.activity.UserFeedbackActivity;
import com.wei.duowan.bbs.ui.activity.VideoDownloadActivity;
import com.wei.duowan.bbs.ui.activity.WebViewActivity;
import com.wei.duowan.bbs.ui.adapter.BaseDelegateAdapter;
import com.wei.duowan.bbs.ui.adapter.BaseViewHolder;

import java.util.Arrays;

import butterknife.Bind;

/**
 * 作者：赵若位
 * 时间：2019/8/1 15:11
 * 联系方式：1070138445
 * 功能：首页
 */
public class MineFragment extends BaseFragment
{
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private BaseDelegateAdapter mUserAdapter, mMenuAdapter, mFunctionAdapter;
    private DelegateAdapter mAdapter;


    @Override
    protected BasePresenter createPresenter()
    {
        return null;
    }

    @Override
    protected int getLayoutID()
    {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView()
    {
        VirtualLayoutManager manager = new VirtualLayoutManager(mActivity);
        ((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new DelegateAdapter(manager, false);
        mRecyclerView.setAdapter(mAdapter);

        mUserAdapter = new BaseDelegateAdapter<String>(mActivity, R.layout.layout_user)
        {
            @Override
            protected void convert(@Nullable BaseViewHolder holder, int position, @Nullable String s)
            {
                holder.setVisibility(R.id.group_login, View.GONE)
                        .setVisibility(R.id.group_nologin, View.VISIBLE)
                        .itemView.setOnClickListener(view -> MineFragment.this.mActivity.alert("登录中..."));
            }
        };
        mAdapter.addAdapter(mUserAdapter);
        mUserAdapter.setNewData("123");


        mMenuAdapter = new BaseDelegateAdapter<String>(mActivity, R.layout.layout_user_menu)
        {
            @Override
            protected void convert(@Nullable BaseViewHolder holder, int position, @Nullable String s)
            {
                holder.setImageResource(R.id.img_menu, R.mipmap.user_message)
                        .setText(R.id.tv_menu, "每日抽奖")
                        .itemView.setOnClickListener(view ->
                {
                    switch (position)
                    {
                        case 0://每日抽奖
                            MineFragment.this.mActivity.openActivity(WebViewActivity.class);
                            break;
                        case 1://我的好友
                            MineFragment.this.mActivity.openActivity(FriendActivity.class);
                            break;
                        case 2://关注动态
                            MineFragment.this.mActivity.openActivity(DynamicActivity.class);
                            break;
                        case 3://我的帖子
                            MineFragment.this.mActivity.openActivity(PostActivity.class);
                            break;
                        case 4://签到日历
                            MineFragment.this.mActivity.openActivity(WebViewActivity.class);
                            break;
                        case 5://我的收藏:
                            MineFragment.this.mActivity.openActivity(FavoriteActivity.class);
                            break;
                        case 6://我的关注
                            MineFragment.this.mActivity.openActivity(AttentionActivity.class);
                            break;
                        case 7://我的板块:
                            MineFragment.this.mActivity.openActivity(PlateActivity.class);
                            break;
                    }
                });
            }

            @Override
            public LayoutHelper setLayoutHelper()
            {
                GridLayoutHelper helper = new GridLayoutHelper(4);
                helper.setBgColor(getResources().getColor(R.color.colorf5));
                helper.setMarginBottom(getResources().getDimensionPixelOffset(R.dimen.dp_16));
                return helper;
            }
        };
        mAdapter.addAdapter(mMenuAdapter);

        mMenuAdapter.setNewData(Arrays.asList(new String[]{
                "1",
                "2",
                "3",
                "4",
                "5",
                "6",
                "7",
                "8",
        }));

        mFunctionAdapter = new BaseDelegateAdapter<String>(mActivity, R.layout.layout_user_function)
        {
            @Override
            protected void convert(@Nullable BaseViewHolder holder, int position, @Nullable String s)
            {
                holder.setText(R.id.tv_title, "视频下载")
                        .itemView.setOnClickListener(view ->
                {
                    switch (position)
                    {
                        case 0://视频下载
                            MineFragment.this.mActivity.openActivity(VideoDownloadActivity.class);
                            break;
                        case 1://用户反馈
                            MineFragment.this.mActivity.openActivity(UserFeedbackActivity.class);
                            break;
                        case 2://设置
                            MineFragment.this.mActivity.openActivity(SettingActivity.class);
                            break;
                        case 3://切换账号
                            MineFragment.this.mActivity.openActivity(AccountManagerActivity.class);
                            break;
                    }
                });
            }
        }.setIsEmpty(true);
        mAdapter.addAdapter(mFunctionAdapter);
//
        mFunctionAdapter.setNewData(Arrays.asList(new String[]{
                "1",
                "2",
                "3",
                "4",
        }));
    }
}
